﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;

namespace CrmApplication.Models
{
    public class DivisionModels
	{
		#region RegionMappingDivision
		
		public decimal Id{ get; set; }
        public string Name { get; set; }

		#endregion

		#region RegionOracleConnect

		private static OracleConnection connect;

        static DivisionModels()
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["HomeOrcl"].ConnectionString;
            connect = new OracleConnection(connString);
        }

		#endregion

		#region RegionDivision
		/// <summary>
        /// select all divisions for combobox
        /// </summary>
        /// <returns></returns>
        public static List<DivisionModels> GetDivision(string searchDiv)
        {
            string selectDiv = "where division_name like '" + searchDiv + "%'";
            string selectString = "select id_division,division_name from division "
									+ selectDiv
									+ " order by division_name ";

            OracleCommand loginComm = new OracleCommand(selectString);
            loginComm.Connection = connect;

            List<DivisionModels> result = new List<DivisionModels>();
            DivisionModels result1 = null;
            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result1 = new DivisionModels();
                        result1.Id = sdr.GetDecimal(0);
                        result1.Name = sdr.GetString(1);                        
                        result.Add(result1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }

        /// <summary>
        /// add new division
        /// </summary>
        /// <param name="countyName"></param>
        public static void AddDivision(string divName)
        {
            string insert = "insert into division(division_name) values (:divName)";

            OracleCommand usercomm = new OracleCommand(insert);

            usercomm.Parameters.Add(":divName", divName);

            usercomm.Connection = connect;
            try
            {
                connect.Open();
                usercomm.ExecuteNonQuery();
            }
            finally
            {
                connect.Close();
            }
        }

        /// <summary>
        /// check if division exists
        /// </summary>
        /// <param name="division"></param>
        /// <returns></returns>
        public static DivisionModels CheckDiv(string division)
        {
            string selectString = "select id_division,division_name "
									+ " from division where division_name=:divisionName ";

            OracleCommand loginComm = new OracleCommand(selectString);
            loginComm.Connection = connect;

            loginComm.Parameters.Add(":divisionName", division);

            DivisionModels result = null;

            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result = new DivisionModels();
                        result.Id= sdr.GetDecimal(0);
                        result.Name = sdr.GetString(1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }

        /// <summary>
        /// creat division array for json
        /// </summary>
        /// <param name="searchCounty"></param>
        /// <returns></returns>
        public static DivisionContainer GetDivArray(string searchDiv)
        {
            DivisionModels[] data = new DivisionModels[20];
            data = DivisionModels.GetDivision(searchDiv).ToArray();

            DivisionContainer container = new DivisionContainer()
            {
                Records = data,
                Count = data.Length
            };

            return container;
		}
		#endregion
	}

    public class DivisionContainer
    {
        public DivisionModels[] Records { get; set; }
        public int Count { get; set; }
    }
}